package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.RoleDao;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;

    @Autowired
    private RoleDao roleDao ;

    @Override
    public User login(String username, String password) {
        User user = userDao.selectOne(username,password) ;
        if (! ObjectUtils.isEmpty(user)) {
            try {
                user.setRoles(roleDao.selectRolesByUserId(user.getUserId()));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return user ;
    }

    @Override
    public List<User> findUserByName(String username) {
        return userDao.selectByName(username) ;
    }
}
